package com.java46.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.entity.News;
import com.java46.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Collections;
import java.util.List;

/**
 * @ClassName NewsService
 * @Description 新闻查询的service层
 * @Author Tigger Wu
 * @Date 2018/8/1817:14
 * @Version 1.0
 **/
@Service
@Transactional
public class NewsService {

    @Autowired
    private NewsMapper newsMapper;
    /*
     *  查询全部新闻
     *
     * @author Tigger Wu
     * @date 2018/8/18 17:26
     * @param
     * @return
     */

    public PageInfo<News> queryList(Integer page){
        //PageHelper.startPage(page, pageSize);这段代码表示，程序开始分页了，
        // page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page,3);
        List<News>nlist = newsMapper.selectAll();
        //调用插件里的分页实体
        PageInfo<News> p = new PageInfo<News>(nlist);
        return p;
    }
/*
*
 *  搜索最近的6条信息
 *
 * @author Tigger Wu
 * @date 2018/8/20 17:33
 * @param []
 * @return java.util.List<com.java46.entity.News>
 */
    public List<News> quertLastNews(){
        List<News>newsList = newsMapper.selcetByTime();
        return newsList;
    }
    /*
    *
     *  根据id查询新闻
     *
     * @author Tigger Wu
     * @date 2018/8/20 18:09
     * @param
     * @return
     */
    public News queryById(Integer id){
        News news = newsMapper.selectByPrimaryKey(id);
        if(news ==null){
            throw new RuntimeException("数据异常，请稍后再访问！");
        }
        return  news;
    }
}
